package com.luckmgr.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.common.constant.SystemConstant;
import com.common.entity.PageBean;
import com.common.utils.StringUtils;
import com.luckmgr.entity.db.Activity;
import com.luckmgr.entity.db.ActivityLevel;
import com.luckmgr.entity.db.CountUser;
import com.luckmgr.entity.db.User;
import com.luckmgr.service.ActivityLevelService;
import com.luckmgr.service.ActivityService;
import com.luckmgr.service.LuckUserService;

@Controller
@RequestMapping("/actlevel")
public class ActivityLevelController {
	
	@Autowired
	ActivityLevelService activityLevelServiceImpl;
	@Autowired
	ActivityService activityServiceImpl;
	@Autowired
	LuckUserService luckUserService;
	
	@RequestMapping(value = "")
	public String main(Model model,HttpSession session,Integer aid,Integer page) {
		if(null == page)
			page = 1;
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("aid", aid);
		PageBean<ActivityLevel> pagebean = activityLevelServiceImpl.selectListPage(map, page);
		
		List<CountUser> countUsers = luckUserService.selectCountUser(aid);
		Map<Integer, CountUser> countUsermap = new HashMap<Integer, CountUser>();
		if(null != countUsers){
			for(CountUser countUser : countUsers){
				countUsermap.put(countUser.getLid(), countUser);
			}
		}
		
		if(null != pagebean.getData()){
			for(ActivityLevel activityLevel: pagebean.getData()){
				CountUser countUser = countUsermap.get(activityLevel.getId());
				if(null != countUser){
					activityLevel.setConutuser(countUser.getConutuser());
				}
			}
		}
		
		List<Activity> activitys = activityServiceImpl.selectList(null);
		
		model.addAttribute("pagebean", pagebean);
		model.addAttribute("activitys", activitys);
		model.addAttribute("aid", aid);
		return "actlevel/index";
	}
	
	@RequestMapping(value = "addup")
	public String addup(Model model,Integer id) {
		ActivityLevel prize = null;
		if(null !=id){
			prize = activityLevelServiceImpl.selectByPrimaryKey(id);
			model.addAttribute("entity", prize);
		}
		
		List<Activity> activitys = activityServiceImpl.selectList(null);
		model.addAttribute("activitys", activitys);
		
		return "actlevel/addup";
		
	}
	
	@RequestMapping(value = "upblack")
	public String upblack(Model model) {
		List<Activity> activitys = activityServiceImpl.selectList(null);
		model.addAttribute("activitys", activitys);
		return "actlevel/upblack";
		
	}


	
	@RequestMapping(value = "delete")
	public String comdelete(Model model,@RequestParam(required = false)Integer id) {
		if(null != id && 0 != id){
			
			ActivityLevel activityLevel = activityLevelServiceImpl.selectByPrimaryKey(id);
			if(null == activityLevel){
				model.addAttribute("msg", "所选活动组不存在！");
				return "actlevel/addup";
			}
			
			Activity activity = activityServiceImpl.selectByPrimaryKey(activityLevel.getActivityId());
			if(null !=activity && activity.getStatus()==1){
				model.addAttribute("msg", "活动已开始，不能进行此操作!");
				return "actlevel/addup";
			}
			
			activityLevelServiceImpl.deleteLevel(id);
		}
		return "redirect:/actlevel";
	}

	
	
	@RequestMapping(value = "update")
	public String update(Model model,HttpSession session,@ModelAttribute("entity") ActivityLevel prize) {
		User user = (User) session.getAttribute(SystemConstant.ESSIONUSER);
		if(null == prize.getLotteryNum() 
				|| (null == prize.getActivityId() && prize.getId()==null)){
			model.addAttribute("msg", "数据不完整请重新录入！");
			return "actlevel/addup";
		}
		
		
		prize.setOperatorName(user.getUserName());
		prize.setOperatorIp(user.getLoginIp());
		
		if(prize.getId()!=null && 0!=prize.getId()){
			ActivityLevel temprize = activityLevelServiceImpl.selectByPrimaryKey(prize.getId());
			if(null ==temprize){
				model.addAttribute("msg", "该活动已经被移除");
				return "actlevel/addup";
			}
			Activity activity = activityServiceImpl.selectByPrimaryKey(temprize.getActivityId());
			if(null ==activity || activity.getStatus()==1){
				model.addAttribute("msg", "活动已开始或已删除，不能进行此操作!");
				return "actlevel/addup";
			}
			activityLevelServiceImpl.updateEntity(prize);
		}else{
			Activity activity = activityServiceImpl.selectByPrimaryKey(prize.getActivityId());
			if(null ==activity || activity.getStatus()==1){
				model.addAttribute("msg", "活动已开始，不能进行此操作!");
				return "actlevel/addup";
			}
			
			if(StringUtils.isNull(prize.getLevelName())){
				model.addAttribute("msg", "数据不完整请重新录入！");
				return "actlevel/addup";
			}
			prize.setActivityName(activity.getName());
			activityLevelServiceImpl.addEntity(prize);
		}
		return "redirect:/actlevel";
	}
	
	
	@RequestMapping(value = "updateblack")
	public String updateblack(Model model,HttpSession session,Integer aid,String users) {
		if(StringUtils.isNull(users) || null == aid || 0 == aid){
			model.addAttribute("msg", "数据不完整请重新录入！");
			return "actlevel/upblack";
		}
		
		Activity activity = activityServiceImpl.selectByPrimaryKey(aid);
		if(null ==activity || activity.getStatus()==1){
			model.addAttribute("msg", "活动已开始或已删除，不能进行此操作!");
			return "actlevel/addup";
		}
		
		activityLevelServiceImpl.updateBlackUser(aid, users);
		model.addAttribute("msg", "更新成功！");
		return "actlevel/upblack";
	}
	
	
	
}
